﻿/*!	@file	r_resource_server.cpp
	@brief	リソース管理クラス
Copyright (c) 2010 Yuya Yamaguchi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include "r_resource_server.h"
#include "r_resource_loader.h"
#include "r_resource.h"

#include "../alloc/r_alloc_buffer.h"
#include "../stream/r_stream_default.h"
#include "../r_debug.h"

namespace r{
	namespace resource{
		RSINGLETONGET_IMPLEMENT ( Server, Server )

		Server :: Server()
		{
		}
		Server :: ~Server()
		{
		}

		ResourcePtr		Server :: loadSync ( const r_cstr	fileName )
		{
			RDEBUG_INFO_MSG( _RST("load request[%s]"), fileName );
			ResourcePtr resPtr = findResource( fileName );
			if( r::resource::isEnable( resPtr ) ){
				return resPtr;
			}
			r::stream::ReaderDefault		stream ( fileName );

			for( LoaderArray::iterator it = loaders.begin(); it != loaders.end(); it++ ){
				Loader* loader = (*it);
				Resource* res = loader->load( fileName, &stream );
				if( res != NULL ){
					ResourcePtr resPtr( new ResourceProxy(res) );
					resources.push_back( resPtr );
					return resPtr;
				}
			}
			RDEBUG_WARRN( _RST("未対応ファイル %s"), fileName );
			return failedResource;
		}

		ResourcePtr	Server :: asignResource ( const r_cstr fileName, Resource* res )
		{
			RDEBUG_INFO_MSG( _RST("asign to [%s]"), fileName );
			{
				ResourcePtr resPtr = findResource( fileName );
				if( r::resource::isEnable( resPtr ) ){
					RDEBUG_WARRN(_RST("found"));
				}
			}
			ResourcePtr resPtr( new ResourceProxy(res) );
			resources.push_back( resPtr );
			return resPtr;
		}

		ResourcePtr	Server :: findResource ( const r_cstr fileName )
		{
			// 文字列をハッシュにして検索
			return findResource( cRHash64( fileName ) );
		}

		ResourcePtr	Server :: findResource ( const cRHash64& fileName )
		{
			// 本来はロック開始
			ResourceArray::iterator it = std::find_if( resources.begin(), resources.end(), [&]( ResourcePtr& res ){ return ( r::resource::isEnable(res) ? (fileName == res->get()->getHash() ? true : false) : false ); } );
			// ロック終了
			if( it != resources.end() ){
				return *it;
			}
			return failedResource;
		}

		void	Server :: addResourceLoader( Loader* loader )
		{
			LoaderArray::iterator it = std::find_if( loaders.begin(), loaders.end(), [&]( Loader* l ){ return ( r::rtti::getRTTI(*l) == r::rtti::getRTTI(*loader) ? true : false); } );
			RDEBUG_ASSERT( it == loaders.end() );
			loaders.push_back( loader );
		}

		void	Server :: update()
		{
			// 本来はロック開始

			// ロードリストの更新
			// 読み込み済みデータは破棄
			// セットアップリストに詰め込み

			// 参照カウンタ1は破棄
			for( ResourceArray::iterator it = resources.begin(); it != resources.end(); ){
				if( (*it).use_count() == 1 ){
					it = resources.erase( it );
				}else{
					it++;
				}
			}

			// ロック終了
		}

	}
}